<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------

namespace app\model\user;


use crmeb\basic\BaseModel;
use crmeb\traits\ModelTrait;
use think\Model;
use think\facade\Db;

/**
 * Class UserInvoice
 * @package app\model\live
 */
class UserInvoice extends BaseModel
{
    use ModelTrait;

    protected $pk = 'id';

    protected $name = 'user_invoice';

    protected $autoWriteTimestamp = 'int';

    protected $createTime = 'add_time';

    protected function setAddTimeAttr()
    {
        return time();
    }

    /**
     * 添加时间获取器
     * @param $value
     * @return false|string
     */
    public function getAddTimeAttr($value)
    {
        if (!empty($value)) {
            return date('Y-m-d H:i:s', (int)$value);
        }
        return '';
    }


    /**
     * @param Model $query
     * @param $value
     */
    public function searchUidAttr($query, $value)
    {
        if ($value !== '') $query->where('uid', $value);
    }

    /**
     * @param Model $query
     * @param $value
     */
    public function searchHeaderTypeAttr($query, $value)
    {
        if ($value !== '') $query->where('header_type', $value);
    }

    /**
     * @param Model $query
     * @param $value
     */
    public function searchTypeAttr($query, $value)
    {
        if ($value !== '') $query->where('type', $value);
    }

    /**
     * @param Model $query
     * @param $value
     */
    public function searchIsDefaultAttr($query, $value)
    {
        if ($value !== '') $query->whereLike('is_default', $value);
    }

    /**
     * @param Model $query
     * @param $value
     */
    public function searchIsDelAttr($query, $value)
    {
        if ($value !== '') $query->where('is_del', $value);
    }



    public static function syncUserInvoice($invoiceInfo)
    {
        $add = [
            "user_receipt_id"   => $invoiceInfo['user_receipt_id'] ?? '',
            "receipt_type"      => $invoiceInfo['type'] ?? '',
            "receipt_title"     => $invoiceInfo['name'] ?? '',
            "receipt_title_type" => $invoiceInfo['header_type'] ?? '',
            "duty_paragraph"    => $invoiceInfo['duty_number'] ?? '',
            "email"             => $invoiceInfo['email'] ?? '',
            "bank_name"         => $invoiceInfo['bank'] ?? '',
            "bank_code"         => $invoiceInfo['card_number'] ?? '',
            "address"           => $invoiceInfo['address'] ?? '',
            "tel"               => $invoiceInfo['tell'] ?? '',
            "is_default"        => $invoiceInfo['is_default'] ?? '',
            "uid"               => $invoiceInfo['uid'] ?? '',
            "create_time"       => date("Y-m-d H:i:s"),
        ];
//        return Db::connect('mysql2')->table('eb_user_receipt')->insert($add);
    }



    public static function syncUpdateUserInvoice($id, $invoiceInfo)
    {
        if(!empty($invoiceInfo["uid"]))             $update["uid"] = $invoiceInfo["uid"];
        if(!empty($invoiceInfo["type"]))            $update["receipt_type"] = $invoiceInfo["type"];
        if(!empty($invoiceInfo["name"]))            $update["receipt_title"] = $invoiceInfo["name"];
        if(!empty($invoiceInfo["header_type"]))     $update["receipt_title_type"] = $invoiceInfo["header_type"];
        if(!empty($invoiceInfo["duty_number"]))     $update["duty_paragraph"] = $invoiceInfo["duty_number"];
        if(!empty($invoiceInfo["email"]))           $update["email"] = $invoiceInfo["email"];
        if(!empty($invoiceInfo["bank"]))            $update["bank_name"] = $invoiceInfo["bank"];
        if(!empty($invoiceInfo["card_number"]))     $update["bank_code"] = $invoiceInfo["card_number"];
        if(!empty($invoiceInfo["address"]))         $update["address"] = $invoiceInfo["address"];
        if(!empty($invoiceInfo["tell"]))            $update["tel"] = $invoiceInfo["tell"];
        if(!empty($invoiceInfo["detail"]))          $update["detail"] = $invoiceInfo["detail"];
        if(!empty($invoiceInfo["is_default"]))      $update["is_default"] = $invoiceInfo["is_default"];

//        return Db::connect('mysql2')->table('eb_user_receipt')->where("user_receipt_id", $id)->update($update);
    }


    public static function syncSetUserInvoice($uid, $receipt_title_type, $receipt_type, $id)
    {
//        Db::connect('mysql2')->table('eb_user_receipt')->where('uid', $uid)->where("receipt_title_type", $receipt_title_type)->where('receipt_type', $receipt_type)->update(['is_default' => 0]);
//        return Db::connect('mysql2')->table('eb_user_receipt')->where('user_receipt_id', $id)->update(['is_default' => 1]);
    }






}
